package netty.rpc.framework.core.netty.registry.impl;

import lombok.extern.slf4j.Slf4j;
import netty.rpc.framework.core.netty.registry.ServiceRegistry;
import netty.rpc.framework.core.netty.registry.ZKUtils;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.springframework.util.StringUtils;

/**
 * @author guiyiqiao
 * @date 2020/12/10
 */
@Slf4j
public class ZKServiceRegistryImpl implements ServiceRegistry {

    /**
     * 添加服务中心节点，若不存在则新建一个服务节点,然后为该节点添加服务地址子节点
     *
     * @param serviceName
     * @param serviceUrl
     */
    @Override
    public void registerService(String serviceName, String serviceUrl) {

        if(StringUtils.isEmpty(serviceName) || serviceUrl.isEmpty()){
            return;
        }
        CuratorFramework zkClient = ZKUtils.getZkClient();
        ZKUtils.createPersistentNode(zkClient,serviceName);
        ZKUtils.createEphemeralNode(zkClient,serviceName,serviceUrl);
        log.info("服务{} {} 注册成功",serviceName,serviceUrl);
    }
}
